package com.power.sifa.approve.model.doc;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.plf.core.base.model.EnumerationHelper;
import com.power.sifa.approve.model.enumeration.Tp_applicant;
import com.power.sifa.approve.model.enumeration.Tp_procmaterial;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @ClassName ExecuteAuditForceDoc
 * @Author xiongzw
 * @Date 2023/11/24 12:42
 * @Description 行政复议强制执行决定书
 * @Version: 1.0
 **/
@Data
public class ExecuteAuditForceDoc extends AbstractDoc {

    /**
     * 被申请人
     */
    private String passiveinfo;
    /**
     * 正文
     */
    private String statement;



    private final static String CONTENT_TEMPLATE = "    被执行人{apl_admreview_applyname}因不服{apl_admreview_targetname}作出的[{behavior}]向本府申请行政复议，本府于{decisiondate}作出了《行政复议决定书》{prcode}变更了原行政行为，并于{arrivedate}送达被执行人{apl_admreview_applyname}，" +
            "但被执行人逾期未起诉又不履行行政复议决定，经本府催告后仍不履行行政复议决定，根据《中华人民共和国行政复议法》第三十三条第（二）项（以及引用相应法律法规）规定，本府决定于{forcedate}起依法强制执行，或者申请人民法院强制执行";


    public ExecuteAuditForceDoc() {
        super();
        this.setDocType(Tp_procmaterial.ARRIVE_FORCE);
    }

    public ExecuteAuditForceDoc(String applyid) {
        super(applyid);
        this.setDocType(Tp_procmaterial.ARRIVE_FORCE);
    }

    public ExecuteAuditForceDoc(String applyid, String passiveinfo, String statement) {
        super(applyid);
        this.setDocType(Tp_procmaterial.ARRIVE_FORCE);
        this.passiveinfo = passiveinfo;
        this.statement = statement;
    }

    @Override
    public AbstractDoc generateTemplate(String applyid) {
        ExecuteAuditForceDoc template = new ExecuteAuditForceDoc(applyid);
        // 案号
        template.setApl_admre_audit_code(this.getApl_admre_audit_code());
        StringBuffer content = new StringBuffer();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatT = new SimpleDateFormat("yyyy年M月d日");
        EnumerationHelper helper =  SpringUtil.getBean(EnumerationHelper.class);
        if (CollectionUtil.isNotEmpty(this.getApplicantList())) {
            StringBuffer applyBuffer = new StringBuffer();
            this.getApplicantList().stream().forEach(element -> {
                // 公民
                if (element.getType().equals(Tp_applicant.CITIZEN)) {
                    applyBuffer.append(element.getName());
                    if (ObjectUtil.isNotNull(element.getGender())) {
                        applyBuffer.append("，" + helper.getDescription(element.getGender()));
                    }
                    if(ObjectUtil.isNotNull(element.getNation())) {
                        applyBuffer.append("，" + helper.getDescription(element.getNation()));
                    }
                    if(StringUtils.isNotEmpty(element.getBirthym())) {
                        try {
                            applyBuffer.append("，" + formatT.format(format.parse(element.getBirthym())));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    if(StringUtils.isNotEmpty(element.getAddr())) {
                        applyBuffer.append("，" + element.getAddr());
                    }
                    applyBuffer.append("\n");
                }
                // 企业
                if (element.getType().equals(Tp_applicant.UNIT)) {
                    applyBuffer.append(element.getName());
                    if(StringUtils.isNotEmpty(element.getAddr())) {
                        applyBuffer.append("，" + element.getAddr());
                    }
                    if(StringUtils.isNotEmpty(element.getManager())) {
                        applyBuffer.append("，" + element.getManager());
                    }
                    if(StringUtils.isNotEmpty(element.getJob())) {
                        applyBuffer.append("，" + element.getJob());
                    }
                    applyBuffer.append("\n");
                }
            });
            content.append(applyBuffer);
        }
        template.setPassiveinfo(content.toString());

        String templateContent = CONTENT_TEMPLATE.replace("{apl_admreview_applyname}", ObjectUtil.isNotNull(this.getAdmreObj()) ?
                this.getAdmreObj().getApplyname() : "" );
        templateContent = templateContent.replace("{apl_admreview_targetname}", ObjectUtil.isNotNull(this.getAdmreObj()) ?
                this.getAdmreObj().getTargetname() : "" );
        templateContent = templateContent.replace("{behavior}", this.transBehavior());
        templateContent = templateContent.replace("{prcode}", this.transPrcode());
        templateContent = templateContent.replace("{today}", this.transModelToday(Boolean.FALSE));
        templateContent = templateContent.replace("{decisiondate}",this.transModelToday(Boolean.FALSE));
        templateContent = templateContent.replace("{arrivedate}",this.transModelToday(Boolean.FALSE));
        templateContent = templateContent.replace("{forcedate}",this.transModelToday(Boolean.FALSE));

        template.setStatement(templateContent);
        return template;
    }

}
